<?
$PageTitle=$msg["Page_AddNews"];

include "div_menu.php";

if (!check_admin_access())
  {return;}


if (!$_POST) 
{
  $news = array("date"=>current_time_asstr());
  make_form( $news );
}
else
{
  $news = array("date"=>$_POST["date"], "text"=>stripslashes($_POST["text"]));

  if (isset($_POST["preview"]))
  {
    make_form($news);

    print "<div class=\"preview\">\n";
    print "<div class=\"preview_title\">{$msg["Preview"]}</div>\n";
    print_news($news);
    print "</div>\n";
  }
  else
  {
    if (add_news($news)) 
    {
      print_continue($msg["NewsAdded"], "news");
    } 
    else 
    {
      make_form( $news );
    };
  }
};


//-----------------------------------------------------------------------------

function make_form($news) 
{
  global $msg;
?>
<!-- Add news form -->

<div class="form_area">
<div class="form_div">
<form action="newsadd" method="post">

  <fieldset class="fields1">

  <dl>
    <dt><label for="date"><?=$msg["Date"];?></label></dt>
    <dd><input type="text" size="25" tabindex="1" name="date" id="date" value="<?=$news["date"];?>"/></dd>
  </dl>

  <dl>
    <dt><label for="text"><?=$msg["Text"];?></label></dt>
    <dd><textarea tabindex="2" rows="10" cols="80" name="text"/><?=$news["text"];?></textarea></dd>
  </dl>

  <dl class="button_div">
    <button type="submit" tabindex="3" name="logon"><?=$msg["Add"];?></button>
    &nbsp;
    <button type="submit" tabindex="4" name="preview"><?=$msg["Preview"];?></button>
    &nbsp;
    <button type="button" tabindex="5" name="cancel" onClick="window.location.href='news'"><?=$msg["Cancel"];?></button>
  </dl>

  </fieldset>

</form>
</div>
</div>

<?php
};


//-----------------------------------------------------------------------------

function add_news($news) 
{
  global $msg;

  $n = 0;

  try 
  {
    db_call();

    $date = $news["date"];
    if ($date == "") 
    {
      $errors[$n++] = $msg["NoDate"];
    }    
    else
    {
      $dt = new DateTime($date);
      $date = $dt->format('Y-m-d H:i:s');
    }

    $text = $news["text"];
    if ($text == "") {
      $errors[$n++] = $msg["NoText"];
    }

    if ($n == 0) {
      db_add_news($date, $text);
    }

  } catch (Exception $e) 
  {
    $errors[$n++] = $e->getMessage();
  }

  if ($n <> 0) {
    print_errors($errors);
    return False;
  }

  return True;
};

?>
